


# to load data
library(rio)

# to process data
library(tidyverse)

library(ggpubr)

# to load WDI data
library(WDI)



# load data about laws and funds
laws <- import("list_laws.csv")
funds <- import("fund_types.csv")


#### GENERATE TABLE 1
funds_by_type <- funds %>%
  select(stabilization,investment,development,savings,pension) %>%
  summarise(across(stabilization:pension, list(sum = ~ sum(as.integer(as.logical(.)))))) 

documents_by_type <- laws %>%
  summarise(across(stabilization:pension, list(sum = ~ sum(.)))) 

countries_by_type <- funds %>%
  select(country,stabilization,investment,development,savings,pension) %>%
  group_by(country) %>%
  summarise(across(stabilization:pension, list(sum = ~ sum(as.integer(as.logical(.)))))) %>%
  summarise(across(stabilization_sum:pension_sum, list(sum = ~ ifelse(.>0, 1, 0)))) %>%
  summarise(across(stabilization_sum_sum:pension_sum_sum, list(sum = ~ sum(.))))

countries_by_type <- funds %>%
  select(country,stabilization,investment,development,savings,pension) %>%
  group_by(country) %>%
  summarise(across(stabilization:pension, list(sum = ~ ifelse(sum(as.integer(as.logical(.)))>0, 1, 0)))) %>%
  summarise(across(stabilization_sum:pension_sum, list(sum = ~ sum(.)))) %>%
  rename_with(~ tolower(gsub("_sum_sum", "_sum", .x, fixed = TRUE)))

table1 <- rbind(funds_by_type,documents_by_type,countries_by_type)
names <- c("Number of funds","Number of documents","Number of countries")
table1 <- cbind(names,table1)
